package com.example.shixun.config;

import com.example.shixun.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MyMVCConfig implements WebMvcConfigurer {

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInterceptor())//注册拦截器
//        .addPathPatterns("/**")  //拦截所有请求
//        .excludePathPatterns("/","/index","/**/login","/**/*.html","/**/*.js","/**/*.css","/**/images/*"); //过滤拦截白名单
        registry.addInterceptor(new AuthenticationInterceptor())//注册拦截器
                .addPathPatterns("/**");

    }

    /**
    * 解决跨域请求
    * @param registry
    * */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /*解决跨域请求的路径*/
        //拦截全部请求路径
        registry.addMapping("/**")
                //是否发送cookie
                .allowCredentials(true)
                //放行哪些原始域
                //.allowedOrigins("*")
                .allowedOriginPatterns("*")
                //放行哪些请求方法
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //放行哪些请求头信息
                .allowedHeaders("*")
                //暴露哪些请求头信息
                .exposedHeaders("*");
    }

}
